package bultin;

import interpret.Aid;

import javax.swing.JComponent;

import code.ErrorCondition;
import code.ObjectC;
import code.TokenType;
import code.Variable;
import code.Variable.varType;

import com.sydwell.service.ArrayPlus;


class String_Bi extends BIClass {

//	bstring.addStatic(new FuncMet("prompt", true, true));
//	bstring.addStatic(new FuncMet("convertTo", true, true));
//	bstring.addStatic(new FuncMet("setBackground", true, true));
//	bstring.addStatic(new FuncMet("getBackground", true, true));
//	bstring.addStatic(new FuncMet("setFont", true, true));
//	bstring.addStatic(new FuncMet("getFont", true, true));
//	bstring.addStatic(new FuncMet("pause", true, true));

	public String_Bi() {
		super("String");
		this.addStatic(new Length());
		this.addStatic(new IndexOf());
		this.addStatic(new SubString());
		this.addStatic(new StartsWith());
		this.addStatic(new ToUpperCase());
		this.addStatic(new ToLowerCase());
		this.addStatic(new Trim());
	}


	
	
	class Length extends BuiltinMethod {
		
		public Length() {
			super("length");
			super.addParameter(varType.STRING);
			super.setReturnType(varType.INTEGER);
		}

		@Override
		public Variable excuteMethod(ObjectC theObj,ArrayPlus<TokenType> argumentList) throws ErrorCondition {
//			try {
//				ErrorCondition.checkArugmentList(argumentList, argumentOptions);
//			} catch (ErrorCondition e) {
//				return returnVariable;
//			}
			String str = argumentList.get(0).getTokenVariable().getStrValue();
			if (str == null) {
				throw new ErrorCondition ("Expected the argument to be of the type String ", Aid.OBJECTS);
			}
			returnVariable.setAndConvert("" + str.length());
			return returnVariable;
		}
		
		@Override
		public String convert2Java(ArrayPlus<TokenType> arguments, TokenType target) {
			// chip = date.length();
			String out = arguments.get(0) + " = " + target + ".length()";
			out = out.replace(";", "");
			out = out.replace("\r", "");
			out = out.replace("\n", "");
			return out;
		}

		@Override
		public String information() {
			String out = "\nWill return the number of characters in the supplied String";
			out += "\nUsage: " ;
			out += "String.length(<String>)"; 
			out += "\nWhere: <String> is the supplied string";
			return out;
		}
	}
	
	class IndexOf extends BuiltinMethod {
		
		public IndexOf() {
			super("indexOf");
			super.addParameter(varType.STRING);
			super.addParameter(varType.STRING);
			super.setReturnType(varType.INTEGER);
		}

		@Override
		public Variable excuteMethod(ObjectC theObj,ArrayPlus<TokenType> argumentList) throws ErrorCondition {
//			try {
//				ErrorCondition.checkArugmentList(argumentList, argumentOptions);
//			} catch (ErrorCondition e) {
//				return returnVariable;
//			}
			String str = argumentList.get(0).getTokenVariable().getStrValue();
			if (str == null) {
				throw new ErrorCondition ("Expected the argument to be of the type String ", Aid.OBJECTS);
			}
			String str2 = argumentList.get(1).getTokenVariable().getStrValue();
			if (str2 == null) {
				throw new ErrorCondition ("Expected the argument to be of the type String ", Aid.OBJECTS);
			}
			int out = str.indexOf(str2);
			returnVariable.setAndConvert("" + out);
			return returnVariable;
		}
		
		@Override
		public String convert2Java(ArrayPlus<TokenType> arguments, TokenType target) {
			// chip = date.length();
			String out = arguments.get(0) + " = " + target + ".indexof()";
			out = out.replace(";", "");
			out = out.replace("\r", "");
			out = out.replace("\n", "");
			return out;
		}

		@Override
		public String information() {
			String out = "\nReturns an index within a supplied string, the index will be the first occurrence of the specified <substring> ";
			out += "\nUsage: " ;
			out += "String.indexOf(<String>, <substring>)"; 
			out += "\nWhere: <String> is the supplied String";
			out += "\nWhere: <substring> is the partial string to find";
			return out;
		}
	}
	
	class SubString extends BuiltinMethod {
		
		public SubString() {
			super("subString");
			super.addParameter(varType.STRING);
			super.addParameter(varType.INTEGER);
			super.addParameter(varType.INTEGER);
			super.setReturnType(varType.STRING);
		}

		@Override
		public Variable excuteMethod(ObjectC theObj,ArrayPlus<TokenType> argumentList) throws ErrorCondition {
//			try {
//				ErrorCondition.checkArugmentList(argumentList, argumentOptions);
//			} catch (ErrorCondition e) {
//				return returnVariable;
//			}
			String str = argumentList.get(0).getTokenVariable().getStrValue();
			if (str == null) {
				throw new ErrorCondition ("Expected the argument to be of the type String ", Aid.OBJECTS);
			}
			int int1 = argumentList.get(1).getTokenVariable().getIntValue();
//			if (str == null) {
//				throw new ErrorCondition ("Expected the argument to be of the type String ", Aid.OBJECTS);
//			}
		
			if (argumentList.size() > 2) {
				int int2 = argumentList.get(2).getTokenVariable().getIntValue();
				returnVariable.setAndConvert("" + str.substring(int1, int2));
			} else {
				returnVariable.setAndConvert("" + str.substring(int1));
			}
	//		returnVariable.setAndConvert(" " + str + " " + int1+ " " + int2);
	//		returnVariable.setAndConvert(" ccc " + str + " " );
			return returnVariable;
		}
		
		@Override
		public String convert2Java(ArrayPlus<TokenType> arguments, TokenType target) {
			// chip = date.length();
			String out = arguments.get(0) + " = " + target + ".length()";
			out = out.replace(";", "");
			out = out.replace("\r", "");
			out = out.replace("\n", "");
			return out;
		}

		@Override
		public String information() {
			String out = "\nWill return part of the current String start with ";
			out += "\nUsage: " ;
			out += "String.subString()"; 
			out += "\nWhere: <object> is a String";
			return out;
		}
	}
	
	class StartsWith extends BuiltinMethod {
		
		public StartsWith() {
			super("startsWith");
			super.addParameter(varType.STRING);
			super.setReturnType(varType.BOOLEAN);
		}

		@Override
		public Variable excuteMethod(ObjectC theObj,ArrayPlus<TokenType> argumentList) throws ErrorCondition {
//			try {
//				ErrorCondition.checkArugmentList(argumentList, argumentOptions);
//			} catch (ErrorCondition e) {
//				return returnVariable;
//			}
			String str = argumentList.get(0).getTokenVariable().getStrValue();
			if (str == null) {
				throw new ErrorCondition ("Expected the argument to be of the type String ", Aid.OBJECTS);
			}
			returnVariable.setAndConvert("" + str.length());
			return returnVariable;
		}
		
		@Override
		public String convert2Java(ArrayPlus<TokenType> arguments, TokenType target) {
			// chip = date.length();
			String out = arguments.get(0) + " = " + target + ".length()";
			out = out.replace(";", "");
			out = out.replace("\r", "");
			out = out.replace("\n", "");
			return out;
		}

		@Override
		public String information() {
			String out = "Returns an index within a supplied string, the index will be the first occurrence of the specified <substring> ";
			out += "\nUsage: " ;
			out += "String.indexOf(<String>, <substring>)"; 
			out += "\nWhere: <String> is the supplied String";
			out += "\nWhere: <substring> is the partial string to find";
			return out;
		}
	}
	
	class ToUpperCase extends BuiltinMethod {
		
		public ToUpperCase() {
			super("toUpperCase");
			super.addParameter(varType.STRING);
			super.setReturnType(varType.STRING);
		}

		@Override
		public Variable excuteMethod(ObjectC theObj,ArrayPlus<TokenType> argumentList) throws ErrorCondition {
//			try {
//				ErrorCondition.checkArugmentList(argumentList, argumentOptions);
//			} catch (ErrorCondition e) {
//				return returnVariable;
//			}
			String str = argumentList.get(0).getTokenVariable().getStrValue();
			if (str == null) {
				throw new ErrorCondition ("Expected the argument to be of the type String ", Aid.OBJECTS);
			}
			returnVariable.setAndConvert("" + str.toUpperCase());
			return returnVariable;
		}
		
		@Override
		public String convert2Java(ArrayPlus<TokenType> arguments, TokenType target) {
			// chip = date.length();
			String out = arguments.get(0) + " = " + target + ".length()";
			out = out.replace(";", "");
			out = out.replace("\r", "");
			out = out.replace("\n", "");
			return out;
		}

		@Override
		public String information() {
			String out = "Returns the upper case of the supplied String";
			out += "\nUsage: " ;
			out += "String.toUpperCase()"; 
			out += "\nWhere: <object> is a String";
			return out;
		}
	}
	
	
	class ToLowerCase extends BuiltinMethod {
		
		public ToLowerCase() {
			super("toLowerCase");
			super.addParameter(varType.STRING);
			super.setReturnType(varType.STRING);
		}

		@Override
		public Variable excuteMethod(ObjectC theObj,ArrayPlus<TokenType> argumentList) throws ErrorCondition {
//			try {
//				ErrorCondition.checkArugmentList(argumentList, argumentOptions);
//			} catch (ErrorCondition e) {
//				return returnVariable;
//			}
			String str = argumentList.get(0).getTokenVariable().getStrValue();
			if (str == null) {
				throw new ErrorCondition ("Expected the argument to be of the type String ", Aid.OBJECTS);
			}
			returnVariable.setAndConvert("" + str.toLowerCase());
			return returnVariable;
		}
		
		@Override
		public String convert2Java(ArrayPlus<TokenType> arguments, TokenType target) {
			// chip = date.length();
			String out = arguments.get(0) + " = " + target + ".length()";
			out = out.replace(";", "");
			out = out.replace("\r", "");
			out = out.replace("\n", "");
			return out;
		}

		@Override
		public String information() {
			String out = "Returns the lower case of the supplied String";
			out += "\nUsage: " ;
			out += "String.toLowerCase()"; 
			out += "\nWhere: <object> is a String";
			return out;
		}
	}
	
	class Trim extends BuiltinMethod {
		
		public Trim() {
			super("trim");
			super.addParameter(varType.STRING);
			super.setReturnType(varType.STRING);
		}

		@Override
		public Variable excuteMethod(ObjectC theObj,ArrayPlus<TokenType> argumentList) throws ErrorCondition {
//			try {
//				ErrorCondition.checkArugmentList(argumentList, argumentOptions);
//			} catch (ErrorCondition e) {
//				return returnVariable;
//			}
			String str = argumentList.get(0).getTokenVariable().getStrValue();
			if (str == null) {
				throw new ErrorCondition ("Expected the argument to be of the type String ", Aid.OBJECTS);
			}
			returnVariable.setAndConvert("" + str.trim());
			return returnVariable;
		}
		
		@Override
		public String convert2Java(ArrayPlus<TokenType> arguments, TokenType target) {
			// chip = date.length();
			String out = arguments.get(0) + " = " + target + ".trim()";
			out = out.replace(";", "");
			out = out.replace("\r", "");
			out = out.replace("\n", "");
			return out;
		}

		@Override
		public String information() {
			String out = "Returns a string based on the supplied String, the spaces at the begining and end of the supplied String";
			out += "\nUsage: " ;
			out += "String.trim(<string>)"; 
			out += "\nWhere: <string> is the suplied String to trim";
			return out;
		}
	}




	@Override
	public JComponent makeJavaComp(String theInstanceName,
			ArrayPlus<TokenType> arguments) {
		// TODO Do not need to implement in Static Classes
		return null;
	}
	
	@Override
	public String getJavaClassName() {
		return "String";
	}
}
